package blog.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import blog.entity.Tag;
import blog.service.TagService;

/**
 * 标签控制层
 * @author 江佳洋
 *
 */
@Controller
@RequestMapping("tag")
public class TagController {
	@Autowired
	private TagService tagService;
	/**
	 * 查询所有标签 ，并计算每个标签含有多少文章
	 * @param map
	 * @return
	 */
	@RequestMapping("/list")
	public String tagList(ModelMap map,@RequestParam(required=false,defaultValue="1")Integer pageIndex,
			@RequestParam(required=false,defaultValue="5")Integer pageSize) {
		PageInfo<Tag> pageInfo = tagService.getPageArticleCountByTagId(pageIndex, pageSize);
		map.put("pageUrlPrefix", "tag/list?pageIndex");
		map.put("pageInfo", pageInfo);
		return "article/article_tag_list";
	}
	/**
	 * 添加标签
	 * @param map
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/insertTag")
	public String taginsertTag(ModelMap map) {
		return "article/article_tag_list";
	}
	/**
	 * 判断标签名是否重复
	 * @param map
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/tagName")
	@ResponseBody
	public void tagTagName(ModelMap map,String tagName,HttpServletResponse response) throws IOException {
		Tag tagByName = tagService.getTagByName(tagName);
		if(tagByName!=null) {
			response.setContentType("text/html;charset=utf8");
			response.getWriter().print("1");
		}
	}
	/**
	 * 添加标签
	 * @param map
	 * @param tag
	 * @return  按json行是返回tag  动态添加到前端表格
	 */
	@RequestMapping("/addTag")
	@ResponseBody
	public Tag addTag(ModelMap map,Tag tag){
		tagService.addTag(tag);
		return tag;
	}
	/**
	 *  根据id删除标签
	 * @param map
	 * @param id
	 */
	@RequestMapping("/delete/{id}")
	public String deleteTag(ModelMap map,@PathVariable("id")Integer id,@RequestParam(required=false,defaultValue="1")Integer pageIndex,
			@RequestParam(required=false,defaultValue="5")Integer pageSize){
		tagService.deleteTag(id);
		/*PageInfo<Tag> pageInfo = tagService.getPageArticleCountByTagId(pageIndex, pageSize);
		map.put("pageUrlPrefix", "tag/list?pageIndex");
		map.put("pageInfo", pageInfo);*/
		return "forward:/tag/list";
	}
	/**
	 * 去编辑界面
	 * @param map
	 * @param id
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public String editTag(ModelMap map,@PathVariable("id")Integer id,@RequestParam(required=false,defaultValue="1")Integer pageIndex,
			@RequestParam(required=false,defaultValue="5")Integer pageSize){
		Tag selectTagById = tagService.selectTagById(id);
		map.put("tag", selectTagById);
		/*PageInfo<Tag> pageInfo = tagService.getPageArticleCountByTagId(pageIndex, pageSize);
		map.put("pageUrlPrefix", "tag/list?pageIndex");
		map.put("pageInfo", pageInfo);*/
		map.put("flag", "edit");
		return "forward:/tag/list";
	}
	/**
	 * 保存修改之后的标签
	 * @param map
	 * @param tag
	 * @param pageIndex
	 * @param pageSize
	 * @return 返回1  表示修改成功
	 */
	@RequestMapping("/saveTag")
	@ResponseBody
	public Object saveTag(ModelMap map,Tag tag,@RequestParam(required=false,defaultValue="1")Integer pageIndex,
			@RequestParam(required=false,defaultValue="5")Integer pageSize){
		int updateTag = tagService.updateTag(tag);
		PageInfo<Tag> pageInfo = tagService.getPageArticleCountByTagId(pageIndex, pageSize);
		map.put("pageUrlPrefix", "tag/list?pageIndex");
		map.put("pageInfo", pageInfo);
		return updateTag;
	}
}
